<?php

class AdminController extends Zend_Controller_Action {
	/*
	 * Контейнер для ACL
	 * @var Zend_Acl
	 */

	private $_acl = null;

	/*
	 * Контейнер для db
	 * @var Zend_Db
	 */
	private $_db = null;

	/*
	 * Контейнер для user
	 * @var Zend_Auth
	 */
	private $_user = null;

	/*
	 * Контейнер для user
	 * @var Zend_Auth
	 */
	private $_flashMessenger = null;

	public function init() {
		// Инит ACL
		$this->_acl = Zend_Registry::get('acl');
		// Инит DB
		$this->_db = Zend_Registry::get('db');
		// инит пользователя
		$this->_user = Zend_Auth::getInstance()->getIdentity();
		// Инит flashMessenger
		$this->_flashMessenger = $this->getHelper('flashMessenger');
	}

	public function indexAction() {
		$userId = $this->_user->id;
		$user = new Application_Model_Users();
		$role = $user->getRole($userId);

		if ($this->_acl->isAllowed($role, 'gbAdmin')) {
			$gbConfig = new Zend_Config_Ini(APPLICATION_PATH . '/configs/gb.ini');
			$page = $this->_getParam('page');
			$perpage = $gbConfig->get('perpage');
			$gbModel = new Application_Model_Gb();

			// Настройка постраничного вывода
			$select = $this->_db->select()->from('gb');
			$zpAadapter = new Zend_Paginator_Adapter_DbSelect($select);
			$paginator = new Zend_Paginator($zpAadapter);
			$paginator->setCurrentPageNumber($page)
					->setItemCountPerPage($perpage);

			$this->view->assign('paginator', $paginator);
			$this->view->assign('gbList', $gbModel->getGbList($page, $perpage));
		} else {
			$this->_redirect('/');
		}
	}

	public function deleteAction() {
		$user = new Application_Model_Users();
		$userId = $this->_user->id;
		$role = $user->getRole($userId);
		$gbItemId = $this->_getParam('gbItemId');
		if ($this->_acl->isAllowed($role, 'gbAdmin')) {
			$gbMapper = new Application_Model_Mapper_Gb();
			$gbMapper->delete($gbItemId);
			$this->_flashMessenger->setNamespace('messages')->addMessage('Сообщение с номером ' . $gbItemId . ' удалено');
		}
		$this->_redirect($this->_helper->url(array(), 'adminIndex'));
	}

}

